/**
 @Name：layuiAdmin（iframe版） 公众号管理
 @Author：贤心
 @Site：http://www.layui.com/admin/
 @License：LPPL
 */
layui.define(['admin', 'table', 'util','upload'], function (exports) {
    var $ = layui.$, admin = layui.admin, table = layui.table, element = layui.element, form = layui.form,util = layui.util, upload = layui.upload;
    var addtime = function(d){
        return util.toDateString(d.addtime*1000,'yyyy-MM-dd HH:mm:ss');
    };
    //全部数据
    tableIn = table.render({
        elem: '#list',
        url: '/shengteng/wechat/index',
        method: 'post',
        toolbar: '#toolbar',
        autoSort: false,
        page:true,
        cols: [[
            {field: 'id', title: 'ID', width: 80, fixed: true},
            {field: 'name', title: '公众信息', width: 400, edit:'text'},
            {field: 'type', title: '类型', width: 160,sort:true},
            {field: 'status', align: 'center', title: '状态', width: 100, toolbar: '#status',sort:true},
            {field: 'is_use', align: 'center', title: '接入', width: 150, toolbar: '#use',sort:true},
            {field: 'sort', align: 'center', title: '排序', width: 80, edit:'text',sort:true},
            {field: 'create_time', title: '添加时间',width: 180,sort:true,hide:true},
            {field: 'update_time', title: '修改时间',width: 180,sort:true},
            {width: 200, align: 'center', toolbar: '#action'}
        ]]
    });
    //监听排序事件
    table.on('sort(list)', function(obj){
        table.reload('list', {
            initSort: obj
            ,where: {sort_by: obj.field,sort_order: obj.type}
        });
    });
    //修改字段
    table.on('edit(list)', function(obj){
        var param={};
        param[obj.field] = obj.value;
        param['id'] = obj.data.id;
        cf(param);
    });
    //改变状态
    form.on('switch(status)', function (obj) {
        var param={};
        param[obj.elem.name] = obj.elem.checked===true?1:0;
        param['id'] = this.value;
        cf(param);
    });
    function cf(param){
        admin.req({
            url: '/shengteng/wechat/cf',
            data: param,
            type:'post',
            success: function (res) {
                if(res.code==0){
                    layer.msg(res.msg,{time:1000,icon:1});
                }
                tableIn.reload();
            }
        });
    }
    //改变接入
    form.on('switch(is_use)', function (obj) {


        var param={};
        param[obj.elem.name] = obj.elem.checked===true?1:0;
        param['id'] = this.value;
        admin.req({
            url: '/shengteng/wechat/isUse',
            data: param,
            type:'post',
            success: function (res) {
                if(res.code==0){
                    layer.msg(res.msg,{time:1000,icon:1});
                }
                tableIn.reload();
            }
        });
    });



    //监听工具条
    table.on('tool(list)', function (obj) {
        var data = obj.data;
        var id = data.id;
        if (obj.event === 'del') {
            layer.confirm('您确定要删除该公众号吗？', function () {
                admin.req({
                    url: '/shengteng/wechat/del/'
                    , type: 'post'
                    , data: {'id': id}
                    , success: function (res) {
                        layer.closeAll('dialog');
                        if (res.code == 0) {
                            layer.msg(res.msg, {icon: 1, time: 1000}, function () {
                                obj.del();
                            });
                        }
                    }
                });
            });
        }else if (obj.event === 'edit') {
            var index = layer.open({
                type: 2,
                content: '/shengteng/wechat/edit/id/' + id,
                area: ['300px', '300px'],
                maxmin: true
            });
            layer.full(index);
        }else if (obj.event === 'access') {
            var index = layer.open({
                type: 2,
                content: '/shengteng/wechat/access/id/' + id,
                area: ['300px', '300px'],
                maxmin: true
            });
            layer.full(index);
        }
    });
    //编辑提交
    form.on('submit(edit-btn)', function (data) {
        admin.req({
            url: '/shengteng/wechat/edit/' //实际使用请改成服务端真实接口
            , type: 'post'
            , data: data.field
            , done: function (res) {
                layer.msg(res.msg, {icon: 1, time: 1000}, function () {
                    layer.closeAll("iframe");
                    //刷新父页面
                    parent.location.reload();
                });
            }
        });
    });

    //头工具栏事件
    table.on('toolbar(list)', function(obj){
        switch(obj.event){
            case 'add':
                var index = layer.open({
                    type: 2,
                    content: '/shengteng/wechat/add/',
                    area: ['300px', '300px'],
                    maxmin: true
                });
                layer.full(index);
                break;
        }
    });
    //添加提交
    form.on('submit(add-btn)', function(data){
        admin.req({
            url: '/shengteng/wechat/add/'
            , type: 'post'
            , data: data.field
            , done: function (res) {
                layer.msg(res.msg, {icon: 1, time: 1000}, function () {
                    layer.closeAll("iframe");
                    //刷新父页面
                    parent.location.reload();
                });
            }
        });
    });

    //上传logo
    var logo = $('#logo');
    upload.render({
        url: '/shengteng/Upfiles/upload/'
        , elem: '#logoUpload'
        , done: function (res) {
            if (res.code == 0) {
                logo.val(res.url);
                logo.parent().append('<input type="hidden" name="logo_aid" value="'+res.aid+'">')
            } else {
                layer.msg(res.msg, {icon: 5});
            }
        }
    });
    //查看logo
    admin.events.logoShow = function (othis) {
        var src = logo.val();
        layer.photos({
            photos: {
                "title": "查看图片" //相册标题
                , "data": [{
                    "src": src //原图地址
                }]
            }
            , shade: 0.01
            , closeBtn: 1
            , anim: 5
        });
    };
    //上传二维码
    var qrcode = $('#qrcode');
    upload.render({
        url: '/shengteng/Upfiles/upload/'
        , elem: '#qrcodeUpload'
        , done: function (res) {
            if (res.code == 0) {
                qrcode.val(res.url);
                qrcode.parent().append('<input type="hidden" name="qr_aid" value="'+res.aid+'">')
            } else {
                layer.msg(res.msg, {icon: 5});
            }
        }
    });
    //查看二维码
    admin.events.qrcodeShow = function (othis) {
        var src = qrcode.val();
        layer.photos({
            photos: {
                "title": "查看图片" //相册标题
                , "data": [{
                    "src": src //原图地址
                }]
            }
            , shade: 0.01
            , closeBtn: 1
            , anim: 5
        });
    };

    exports('wechat', {});
});
